package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.Resource;
import com.xszx.server.entity.User;
import com.xszx.server.exception.BusinessException;
import com.xszx.server.exception.BusinessExceptionCode;
import com.xszx.server.mapper.UserMapper;
import com.xszx.server.util.CopyUtil;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserService {
    @javax.annotation.Resource
    private UserMapper userMapper;

    /**
     * 获取分页列表
     * @param page
     * @return
     */
    public Page getAllUsers(Page page){
        int pageNum = page.getPage();
        page.setPage((page.getPage()-1)*page.getSize());
        page.setList(userMapper.findAll(page));
        page.setTotal(userMapper.findCount());
        page.setPage(pageNum);
        return page;
    }

    /**
     * 新增或者删除信息
     * @param user
     * @return
     */
    public String saveOrUpdate(User user){
        if(StringUtils.isEmpty(user.getId())){
            //验证当前用户名是否存在
            User userDb = this.getUserByLoginName(user.getLoginName());
            if(userDb == null){
                user.setId(UuidUtil.getShortUuid());
            }else{
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST) ;
            }
            return (userMapper.insert(user)>0)?"添加成功":"添加失败";
        }else{
            user.setPassword(null);//不更新密码
            return (userMapper.updateByPrimaryKeySelective(user)>0)?"更新成功":"更新失败";
        }
    }
    //更新数据
    public int update(User user){
        return userMapper.updateByPrimaryKey(user);
    }

    /**
     * 物理删除用户
     * @param id
     * @return
     */
    public int del(String id){
        return userMapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个用户
     * @return
     */
    public User getUser(String id){
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取单个用户
     * @return
     */
    public User getUserByLoginName(String loginName){
        return userMapper.selectByLoginName(loginName);
    }

    /**
     * 登录
     */
    public User login(User user){
        //进行二次加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //查询数据库中用户数据
        User userDb = this.getUserByLoginName(user.getLoginName());
        if(userDb == null){//不能登录
            throw  new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            //判断密码是否相等
            if(userDb.getPassword().equals(user.getPassword())){
                return userDb;
            }else{
                throw  new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }

    /**
     * 给用户设置资源
     * @param userId
     */
    public List<Resource> getResources(String userId){
       List<Resource> resources = userMapper.findResourcesByUserId(userId);
       return resources;
    }
}
